package com.tian.controller;

import com.tian.service.ItemsESService;
import com.tian.utils.JSONResult;
import com.tian.utils.PagedGridResult;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("items")
public class ItemsController {

    @Autowired
    private ItemsESService itemsESService;

    @GetMapping("/hello")
    public Object hello(){
        return "Hello Elasticsearch~";
    }


    @GetMapping("/es/search")
    public JSONResult search(
            String keywords,
             String sort,
             Integer page,
             Integer pageSize

    ){
        if (StringUtils.isBlank(keywords)){
            return JSONResult.errorMsg(null);
        }
        if (page==null){
            page=1;
        }
        if (pageSize==null){
            pageSize=20;
        }
        page--; //es从0开始

        PagedGridResult gridResult = itemsESService.searchItems(keywords, sort, page, pageSize);

        return JSONResult.ok(gridResult);
    }
}
